package ${package.ServiceImpl};

import ${package.Mapper}.${table.mapperName};
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import lombok.extern.slf4j.Slf4j;
<#if superServiceImplClass?? && superServiceImplClass == "SuperCacheServiceImpl">
    import org.springframework.aop.framework.AopContext;
    import org.springframework.cache.annotation.CacheConfig;
</#if>
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import ${cfg.VO}.*;
import ${cfg.SaveDTO}.*;
/**
* <p>
    * 业务实现类
    * ${table.comment!?replace("\n","\n * ")}
    * </p>
*
* @author ${author}
* @date ${date}
*/
@Slf4j
@Service
@DS("#thread.tenant")
<#if superServiceImplClass?? && superServiceImplClass == "SuperCacheServiceImpl">
    @CacheConfig(cacheNames = ${table.serviceImplName}.${entity?upper_case})
</#if>
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
}
<#else>
<#if superServiceImplClass??>
    public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
<#else>
    @DS("#thread.tenant")
    public class ${table.serviceImplName} {
</#if>
<#if superServiceImplClass?? && superServiceImplClass == "SuperCacheServiceImpl">
    /**
    * 建议将改变量移动到CacheKey工具类中统一管理，并在 caffeine.properties 文件中合理配置有效期
    */
    protected static final String ${entity?upper_case} = "${entity?lower_case}";

    @Override
    protected String getRegion() {
    return ${entity?upper_case};
    }

    protected ${table.serviceName} currentProxy() {
    return ((${table.serviceName}) AopContext.currentProxy());
    }
</#if>


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<${entity}> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<${entity}> list(${entity}DTO modelDTO, List<Long> ids) {
        return ((${table.serviceName}) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       <#list table.fields as field>
           <#if field.propertyName == "tenantId" || field.propertyName == "projectId"|| field.propertyName == "subsystemId"|| field.propertyName == "isDeleted"><#else>
       "{'${field.propertyName}', #args[0]?.${field.propertyName}},"+
            </#if>
        </#list>
        "{'id', #args[1]}"+
        "}"))
    public List<${entity}> listOfCache(${entity}DTO modelDTO, List<Long> ids) {
       //查询
        ${entity} queryModel = new ${entity}();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO,queryModel);
        }
        QueryWrap<${entity}> wrapper = Wraps.q(queryModel);
        if (ids != null && !ids.isEmpty()) {
            wrapper.lambda().in(${entity}::getId, ids);
        }
        //添加其他业务查询条件
        wrapper.lambda().eq(${entity}::getIsDeleted, BusinessConstant.DELETE_NO);
        List<${entity}> modelList = list(wrapper);
        return modelList;
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public ${entity}VO doToVo(${entity} model) {
        ${entity}VO modelVo = new ${entity}VO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<${entity}VO> doToVos(List<${entity}> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<${entity}VO> result = new ArrayList<>();
        for (${entity} model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
</#if>
